package com.plugin.activity.sample

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.widget.Button
import android.widget.Toast
import java.io.File


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.btnLoadPlugin).setOnClickListener {
            PluginManager.getInstance().loadPlugin(this)
            Toast.makeText(this, "加载插件成功", Toast.LENGTH_SHORT).show()
        }
        findViewById<Button>(R.id.btnStartAct).setOnClickListener {
            val file =
                File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!!.absolutePath + File.separator.toString() + "p.apk")
            Log.e("lyb======", file.absolutePath)
            if(!file.exists()) {
                Toast.makeText(this, "插件不存在", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val path: String = file.absolutePath
            // 获取插件包 里面的 Activity
            val packageInfo =
                packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES)
            val activityInfo = packageInfo!!.activities[0]
            Log.e("lyb======",activityInfo.name)
            // 占位  代理Activity
            val intent = Intent(this, ProxyActivity::class.java)
            intent.putExtra("className", activityInfo.name)
            startActivity(intent)
        }
    }
}